package com.yc.wxcontroller;

import com.yc.entity.*;
import com.yc.mapper.AttendanceMapper;
import com.yc.mapper.AttendanceRecordsMapper;
import com.yc.mapper.ClassInfoMapper;
import com.yc.mapper.ScheduleMapper;
import com.yc.service.*;
import com.yc.vo.AttendanceData;
import com.yc.vo.Result;
import com.yc.vo.StudentAttendanceRecord;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2023/7/24 11:06
 */
@Api("微信管理班级信息控制器")
@RestController
@RequestMapping("/wx/classInfo")
public class WXClassInfoController {
    @Resource
    ClassInfoService classInfoService;

    @Resource
    ClassInfoMapper classInfoMapper;

    @Resource
    ScheduleMapper scheduleMapper;

    @Resource
    StudentInfoService studentInfoService;

    @Resource
    AttendanceRecordsMapper attendanceRecordsMapper;

    @Resource
    AttendanceMapper attendanceMapper;

    @Resource
    ScheduleService scheduleService;

    @Resource
    UserService userService;

    @ApiOperation("通过老师id查询上课的班级")
    @GetMapping(path = "/get/{username}")
    public Result selectCnameByTeacherId(@ApiParam(name = "username", value = "教师ID", required = true)
                                         @PathVariable("username") String username){
        List<ClassInfo> classInfos = classInfoService.selectCnameByTeacherId(username);
        Result result = new Result(1, classInfos);
        return result;
    }

    @ApiOperation("通过学生账号查询上课的班级")
    @GetMapping(path = "/getCname/{username}")
    public Result selectCidByStuid(@ApiParam(name = "username", value = "学生账号", required = true)
                                         @PathVariable("username") String stuid){
        final StudentInfo studentInfo = classInfoMapper.selectCidByStuid(stuid);
        //根据班级id去查询学生
        // 查询学生前判断是否有考勤记录 有考勤记录就联合记录表 将 考勤详情查出来 没有就默认状态为1
        Schedule schedule = scheduleMapper.selectScheduleByCid( studentInfo.getCid() +"");
        Map<String,Object> map = new HashMap<>();
        Integer attId = null;
        Integer count = 0;
        Attendance attendance = null;
        if (schedule!=null){
            attendance = attendanceMapper.selectAttendanceById(schedule.getId() + "");
            if (attendance!=null){
                attId = attendance.getId();
            }
            count = attendanceRecordsMapper.selectAttid(attId);
        }
        List<StudentAttendanceRecord> studentInfoList;
        if (attId == null || attId <= 0){
            return Result.error("教师暂未发布考勤");
        }else if (count>=1){
            return Result.error("已经考勤过");
        }else {
            studentInfoList = studentInfoService.selectStudentInfoByCid(schedule.getId() + "", studentInfo.getCid() + "");
            map.put("attendance",attendance);
            map.put("studentInfoList",studentInfoList);
            return Result.success(map);
        }
    }

    @ApiOperation("查询老师当前所带的班级")
    @GetMapping("/getClasses/{username}")
    public  Result<List<AttendanceData>> queryClasses(@PathVariable("username") String username){

        //获取该老师当前时间段需要上的所有课
        List<AttendanceData> attendanceData = scheduleService.selectClassesAll(username);

        if (attendanceData.size()==0){
            return Result.success(null);
        }else {
            return Result.success(attendanceData);
        }
    }
}
